///////////////////////////////////////////////////////////////////////
// File:	EventSystem.H													 
//																	 
// Author: Roberto Rubiano											 
//																	 
// Purpose: A system to register events and handel events												 
///////////////////////////////////////////////////////////////////////
#ifndef EVENTSYSTEM_H
#define EVENTSYSTEM_H

#include "../Globals.h"
#include "IListener.h"
#include "CEvent.h"

#include <map>
using std::multimap;
using std::pair;

#include <list>
using std::list;

class EventSystem
{
private:
	//	Our Database, this will contain clients that can "listen" for events.
	multimap<EVENTS,IListener*> m_Clients;

	//	Events waiting to be processed.
	list<CEvent> m_CurrEvents;

	//	Utility functions - private because they will only ever be used by this class.
	//
	//	Finds the event in the database and then calls each of the registered client's 
	//	EventHandler functions.
	void DispatchEvent(CEvent* pEvent);
	bool AlreadyRegistered(EVENTS EVENTS, IListener* pClient);

	EventSystem() {};
	EventSystem(const EventSystem&);
	~EventSystem() {};
	EventSystem& operator=(const EventSystem&);

public:

	static EventSystem* GetInstance(void);

	//	This adds a client to the database.  This will make new "buckets" if necessary and then
	//	add the client to that given bucket.
	void RegisterClient(EVENTS EVENTS, IListener* pClient);

	//	Unregisters the client for the specified event only
	void UnregisterClient(EVENTS EVENTS, IListener* pClient);

	//	Removes the client from the database entirely
	void UnregisterClientAll(IListener* pClient);

	//	Sends an event to be processed later on.
	void SendEvent(EVENTS EVENTS, void* pData = NULL);

	//	Processes all events in our event list.
	void ProcessEvents(void);

	//	Clears all pending events
	void ClearEvents(void);

	//	Unregisters all objects
	void ShutdownEventSystem(void);

};

#endif